import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {NzModalService} from 'ng-zorro-antd/modal';
import {Materiaincoming} from '../../../../model/materiaincoming';
import {MateriaincomingeditComponent} from '../materiaincomingedit/materiaincomingedit.component';

import * as uuid from 'uuid';
import {AlertService} from "../../../../services/alert.service";
import {AdminService} from "../../../../services/admin.service";
import {Materiainventory} from '../../../../model/inventory/Materiainventory';

@Component({
  selector: 'app-materiaincominglist',
  templateUrl: './materiaincominglist.component.html',
  styleUrls: ['./materiaincominglist.component.css']
})
export class MateriaincominglistComponent implements OnInit {
  ml: Array<Materiaincoming>
  sl: Array<Materiainventory>;
  inventorytotal = 0;

  constructor(private modal: NzModalService, private viewContainerRef: ViewContainerRef,
              private adminService: AdminService, private alertService: AlertService) {
    this.ml = new Array<Materiaincoming>();
    this.sl = new Array<Materiainventory>();
  }

  ngOnInit(): void {
    this.getMaterians()
    this.loadMateriaInventoryStatic();
  }

  getMaterians() {
    this.adminService.getMatincomings().then((data: any) => {
      if (data.msg == "ok") {
        this.ml = data.list
        this.loadMateriaInventoryStatic();
      } else {
        this.alertService.showError("获取包材入库失败")
      }
    })
  }

  income() {
    const modalwindow = this.modal.create({
      nzTitle: '包材入库信息编辑',
      nzContent: MateriaincomingeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: "add"
      },

      nzOnOk: () => new Promise(resolve => {

        let temp = modalwindow.getContentComponent().m;
        if (!temp.rkrq) {
          this.alertService.showWarning('入库日期必须填写');
          resolve(false)
        }
        else if (!temp.pc) {
          this.alertService.showWarning('进货批次');
          resolve(false);
        } else if (!temp.cpxx) {
          this.alertService.showWarning('产品信息');
          resolve(false);
        } else if (!temp.pm) {
          this.alertService.showWarning('品名必须选择');
          resolve(false);
        } else if (!temp.sl) {
          this.alertService.showWarning('数量必须填写');
          resolve(false);
        } else if (!temp.gg) {
          this.alertService.showWarning('规格必须填写');
          resolve(false);
        } else if (!temp.gys) {
          this.alertService.showWarning('供应商必须选择');
          resolve(false);
        } else if (!temp.cgy) {
          this.alertService.showWarning('采购员必须选择');
          resolve(false);
        }
        else {
          modalwindow.getContentComponent().save()

          setTimeout(() => {

            resolve(true);
            this.getMaterians();
          }, 1000)
        }


      })
    })
  }

  update(id: string) {
    const modalwindow = this.modal.create({
      nzTitle: '包材入库信息编辑',
      nzContent: MateriaincomingeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        optype: "update",
        id: id
      },

      nzOnOk: () => new Promise(resolve => {

        let temp = modalwindow.getContentComponent().m;
        if (!temp.rkrq) {
          this.alertService.showWarning('入库日期必须填写');
          resolve(false)
        } else if (!temp.pc) {
          this.alertService.showWarning('进货批次');
          resolve(false);
        } else if (!temp.cpxx) {
          this.alertService.showWarning('产品信息');
          resolve(false);
        } else if (!temp.pm) {
          this.alertService.showWarning('品名必须选择');
          resolve(false);
        } else if (!temp.sl) {
          this.alertService.showWarning('数量必须填写');
          resolve(false);
        } else if (!temp.gg) {
          this.alertService.showWarning('规格必须填写');
          resolve(false);
        } else if (!temp.gys) {
          this.alertService.showWarning('供应商必须选择');
          resolve(false);
        } else if (!temp.cgy) {
          this.alertService.showWarning('采购员必须选择');
          resolve(false);
        }
        else {
          modalwindow.getContentComponent().save()
          setTimeout(() => {
            resolve(true);
            this.getMaterians();
            this.loadMateriaInventoryStatic();
          }, 1000)
        }


      })
    })
  }

  delete(id: string) {
    this.adminService.deleteMatincomingbyid(id).then((data: any) => {
      if (data.msg == "ok") {
        this.alertService.showSuccess("删除包材入库信息成功")
        this.getMaterians();
      } else {
        this.alertService.showError("删除包材入库信息失败")
      }
    })
  }

  loadMateriaInventoryStatic() {
    this.adminService.getMateriaStaticNum()
      .then((data: any) => {
        if (data.msg == 'ok') {
          this.sl = new Array<Materiainventory>();
          this.sl = data.list;
          this.inventorytotal = 0;
          for (let i = 0; i < data.list.length; i++) {
            this.inventorytotal = this.inventorytotal + data.list[i].total;
          }
        }
        else if (data.msg == 'empty') {
          this.sl = new Array<Materiainventory>();
        }
        else {
          this.alertService.showError('数据获取异常，请重试');
        }
      })
  }

}
